﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

public class Islands
{
    public int beachLength(string[] kingdom)
    {
        var map = kingdom.Select(e => e.ToCharArray()).ToArray();
        int[,] oddNear = { { -1, -1 }, { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 0 } };
        int[,] evenNear = { { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, 0 }, { 1, 1 } };

        int beach = 0;
        for (int i = 0; i < map.Length; i++)
        {
            int[,] near = (i % 2 == 0) ? oddNear : evenNear;
            for (int j = 0; j < map[i].Length; j++)
            {
                if ( map[i][j] != '.' ) continue;
                for (int k = 0 ; k < 6 ; k++ ) {
                    if ( i+near[k,0] < 0 ||  i+near[k,0] >= kingdom.Length ) continue;
                    if ( j+near[k,1] < 0 ||  j+near[k,1] >= kingdom[0].Length ) continue;
                    if ( map[i+near[k,0]][j+near[k,1]] == '#' ) beach++;
                }
            }
        }
        return beach;
    }
}
